package com.food.restaurantpro.fragments;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.imageloader.FileCache;
import com.food.restaurantpro.objects.Promotion;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class PromotionDetailFragment extends Fragment {

	private TextView tvHeader;
	private ImageView mImgvMain;
	private TextView mTvName;
	private TextView mTvDate;
	private TextView mTvTime;
	private TextView mTvInfo;
	
	private FileCache m_FileCache;
	private Promotion mPromotion;
	private Bitmap bitmap;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_promotion_detail, container, false);
		
		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		if(tvHeader != null) {
			tvHeader.setText(R.string.tvPromotion_Title);
		}
		
		mImgvMain = (ImageView) view.findViewById(R.id.imgvPromotionImageMain);
		mTvName = (TextView) view.findViewById(R.id.tvPromotionNameDetails);
		mTvDate = (TextView) view.findViewById(R.id.tvPromotionDateDetails);
		mTvTime = (TextView) view.findViewById(R.id.tvPromotionTimeDetails);
		mTvInfo = (TextView) view.findViewById(R.id.tvPromotionInfoDetails);
		
		m_FileCache = new FileCache(RestaurantApp.CURRENT_CONTEXT);
		// from SD cache
		bitmap = m_FileCache.getBitmapByUrl(mPromotion.getPromotionImageMain());
		mImgvMain.setImageBitmap(bitmap);
		
		mImgvMain.setScaleType(ImageView.ScaleType.FIT_XY);
		mTvName.setText(mPromotion.getPromotionName());
		mTvDate.setText("Date: " + mPromotion.getPromotionDate());
		mTvTime.setText("Time: " + mPromotion.getPromotionTime());
		mTvInfo.setText(mPromotion.getPromotionDescription());
		return view;
	}
	
	public void updateUI(Promotion promotion) {
		mPromotion = promotion;
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mImgvMain.setImageBitmap(null);
		bitmap.recycle();
		m_FileCache = null;
		mPromotion = null;
		Runtime.getRuntime().gc();
	}
}
